﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
   public class GiaoDichDA
    {
       public static GiaoDich GetByID(int maID)
       {
           GiaoDich loai = null;
           SqlConnection cn = DataProvider.Connect();
           SqlCommand cmd = new SqlCommand("Proc_GiaoDich_GetById", cn);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add(new SqlParameter("@maGD", maID));
           SqlDataReader dataReader = cmd.ExecuteReader();
           if (dataReader.Read())
           {
               loai = new GiaoDich();
               loai.MaGD = maID;
               loai.NoiDungGD = Convert.ToString(dataReader["NoiDungGD"]);
               loai.MaTKThe = Convert.ToInt32(dataReader["MaTKThe"]);
               
               loai.MaLoaiGD = Convert.ToInt32(dataReader["LoaiGD"]);
               loai.DiaChiGD = Convert.ToString(dataReader["DiaChiGD"]);
               loai.SoTienGD = Convert.ToSingle(dataReader["SoTienGD"]);
               loai.NgayGD = Convert.ToDateTime(dataReader["NgayGD"]);
               loai.TrangThaiGD = Convert.ToInt32(dataReader["TrangThaiGD"]);

               loai.LoaiGiaoDich = LoaiGDDA.GetByID(loai.MaLoaiGD);
              
            
               loai.tkGiaoDich = TaiKhoanTheDA.GetByID(loai.MaTKThe);


               if (dataReader["MaTKNhan"].ToString() == "")
               {
                   loai.MaNVGD = null;
               }
               else
               {
                   loai.MaNVGD = Convert.ToInt32(dataReader["MaTKNhan"]);

               }

               
              

               if (dataReader["MaNVGD"].ToString() == "")
               {
                   loai.MaNVGD = null;
               }
               else
               {
                   loai.MaNVGD = Convert.ToInt32(dataReader["MaNVGD"]);
                 
               }

               if (dataReader["MaATM"].ToString() == "")
               {
                   loai.MaATM = null;
               }
               else
               {
                   loai.MaATM = Convert.ToInt32(dataReader["MaATM"]);
                   loai.ViTriDatATM = loai.ATMGD.ViTriDat;
               }

               if (dataReader["MaNganHang"].ToString() == "")
               {
                   loai.MaNganHang = null;
               }
               else
               {
                   loai.MaNganHang = Convert.ToInt32(dataReader["MaNganHang"]);
                   loai.TenNganHang = loai.NganHang.TenNganHang;
               }

               if (loai.TrangThaiGD == 1)
                   loai.TrangThai = "Thành công";
               else
                   loai.TrangThai = "Thất bại";

           }
           cn.Close();
           return loai;
       }

       public static List<GiaoDich> GetByMaTKThe(int maTKThe,int type)
       {
           List<GiaoDich> lst = new List<GiaoDich>();
           GiaoDich loai = null;
           SqlConnection cn = DataProvider.Connect();
           SqlCommand cmd = new SqlCommand("Proc_AllGD_ByID", cn);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add(new SqlParameter("@maTKThe", maTKThe));
           cmd.Parameters.Add(new SqlParameter("@type", type));
           SqlDataReader dataReader = cmd.ExecuteReader();
           while (dataReader.Read())
           {
               loai = new GiaoDich();
               loai.MaTKThe = maTKThe;
               loai.NoiDungGD = Convert.ToString(dataReader["NoiDungGD"]);
               loai.MaGD = Convert.ToInt32(dataReader["MaGD"]);
               if (dataReader["MaTKNhan"].ToString() == "")
               {
                   loai.MaTKNhan = null;
               }
               else
               {
                   loai.MaTKNhan = Convert.ToInt32(dataReader["MaTKNhan"]);
               }
               loai.MaLoaiGD = Convert.ToInt32(dataReader["LoaiGD"]);
               loai.DiaChiGD = Convert.ToString(dataReader["DiaChiGD"]);
               loai.SoTienGD = Convert.ToSingle(dataReader["SoTienGD"]);
               loai.NgayGD = Convert.ToDateTime(dataReader["NgayGD"]);
               loai.TrangThaiGD = Convert.ToInt32(dataReader["TrangThaiGD"]);
               loai.LoaiGiaoDich = LoaiGDDA.GetByID(loai.MaLoaiGD);
               //loai.tkGiaoDich = TaiKhoanTheDA.GetByID(loai.MaTKThe);
               loai.TenLoaiGD = loai.LoaiGiaoDich.LoaiGiaoDich;
               if (dataReader["MaNVGD"].ToString() == "")
               {
                   loai.MaNVGD = null;
               }
               else
               {
                   loai.MaNVGD = Convert.ToInt32(dataReader["MaNVGD"]);
               }
               if (dataReader["MaATM"].ToString() == "")
               {
                   loai.MaATM = null;
               }
               else
               {
                   loai.MaATM = Convert.ToInt32(dataReader["MaATM"]);
                   loai.ATMGD = ATMDA.GetByID(loai.MaATM.Value);
                   loai.ViTriDatATM = loai.ATMGD.ViTriDat;
               }
               if (dataReader["MaNganHang"].ToString() == "")
               {
                   loai.MaNganHang = null;
               }
               else
               {
                   loai.MaNganHang = Convert.ToInt32(dataReader["MaNganHang"]);
                   loai.NganHang = NganHangDA.GetByID(loai.MaNganHang.Value);
                   loai.TenNganHang = loai.NganHang.TenNganHang;
               }
               if (loai.TrangThaiGD == 0)
                   loai.TrangThai = "Thành công";
               else if (loai.TrangThaiGD == 1)
                   loai.TrangThai = "Thất bại";
               else if (loai.TrangThaiGD == -1)
                   loai.TrangThai = "Đang chờ";
               lst.Add(loai);

           }
           cn.Close();
           return lst;
       }

       public static bool InsertRutTien(GiaoDich gd,int type)
       {
           try
           {
               SqlConnection cn = DataProvider.Connect();
               SqlCommand cmd = new SqlCommand("Proc_GiaoDich_RutTien", cn);
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.Add(new SqlParameter("@maTKThe", gd.MaTKThe));
               cmd.Parameters.Add(new SqlParameter("@loaiGD", gd.MaLoaiGD));
               cmd.Parameters.Add(new SqlParameter("@ngayGD", gd.NgayGD));
               cmd.Parameters.Add(new SqlParameter("@diaChi", gd.DiaChiGD));
               cmd.Parameters.Add(new SqlParameter("@soTienGD", gd.SoTienGD));
               cmd.Parameters.Add(new SqlParameter("@noiDung", gd.NoiDungGD));
               cmd.Parameters.Add(new SqlParameter("@maNV", gd.MaNVGD));
               cmd.Parameters.Add(new SqlParameter("@trangThai", gd.TrangThai));
               cmd.ExecuteNonQuery();

               cmd = new SqlCommand("Proc_TaiKhoanThe_UpdateSodu", cn);
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.Add(new SqlParameter("@maTKThe", gd.MaTKThe));
               cmd.Parameters.Add(new SqlParameter("@soDu", gd.SoTienGD));
               cmd.Parameters.Add(new SqlParameter("@type", type));
               cmd.ExecuteNonQuery();
           }
           catch (Exception ex) {  }

           return true;
       }

       public static DataTable GetAll(int maTKThe,int trangthai,int type)
       {
           DataTable dt = new DataTable();
           SqlConnection cn = DataProvider.Connect();
           SqlCommand cmd = new SqlCommand("Proc_AllGD_ByID", cn);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add(new SqlParameter("@maTKThe", maTKThe));
           cmd.Parameters.Add(new SqlParameter("@trangthai", trangthai));
           cmd.Parameters.Add(new SqlParameter("@type", type));
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           da.Fill(dt);
           return dt;
       }

       public static bool ChuyenTienTrongNH(GiaoDich gd, int type)
       {
           gd.TrangThaiGD = 0;//chuyen tien thanh cong
           SqlConnection cn = DataProvider.Connect();
           SqlCommand cmd = new SqlCommand("Proc_GiaoDich_ChuyenTienTrongNH", cn);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add(new SqlParameter("@maTKThe", gd.MaTKThe));
           cmd.Parameters.Add(new SqlParameter("@loaiGD", gd.MaLoaiGD));
           cmd.Parameters.Add(new SqlParameter("@ngayGD", gd.NgayGD));
           cmd.Parameters.Add(new SqlParameter("@diaChi", gd.DiaChiGD));
           cmd.Parameters.Add(new SqlParameter("@soTienGD", gd.SoTienGD));
           cmd.Parameters.Add(new SqlParameter("@noiDung", gd.NoiDungGD));
           cmd.Parameters.Add(new SqlParameter("@MaTKNhan", gd.MaTKNhan));
           cmd.Parameters.Add(new SqlParameter("@maNV", gd.MaNVGD));
           cmd.Parameters.Add(new SqlParameter("@trangThai", gd.TrangThaiGD));
           cmd.Parameters.Add(new SqlParameter("@type", type));
           cmd.ExecuteNonQuery();
           return true;
       }

       public static bool ChuyenTienNgoaiNH(GiaoDich gd, int type)
       {
           gd.TrangThaiGD = -1;//cho NV duyet lenh chuyen
           SqlConnection cn = DataProvider.Connect();
           SqlCommand cmd = new SqlCommand("Proc_GiaoDich_ChuyenTienNgoaiNH", cn);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add(new SqlParameter("@maTKThe", gd.MaTKThe));
           cmd.Parameters.Add(new SqlParameter("@loaiGD", gd.MaLoaiGD));
           cmd.Parameters.Add(new SqlParameter("@ngayGD", gd.NgayGD));
           cmd.Parameters.Add(new SqlParameter("@diaChi", gd.DiaChiGD));
           cmd.Parameters.Add(new SqlParameter("@soTienGD", gd.SoTienGD));
           cmd.Parameters.Add(new SqlParameter("@noiDung", gd.NoiDungGD));
           cmd.Parameters.Add(new SqlParameter("@MaTKNhan", gd.MaTKNhan));
           cmd.Parameters.Add(new SqlParameter("@MaNganHang", gd.MaNganHang)); 
           cmd.Parameters.Add(new SqlParameter("@maNV", gd.MaNVGD));
           cmd.Parameters.Add(new SqlParameter("@trangThai", gd.TrangThaiGD));
           cmd.Parameters.Add(new SqlParameter("@type", type));
           cmd.ExecuteNonQuery();
           return true;
       }

       public static DataTable DSChoChuyenTien()
       {
           DataTable dt = new DataTable();
           SqlConnection cn = DataProvider.Connect();
           SqlCommand cmd = new SqlCommand("Proc_AllGD_ChuyenTienNgoaiNH", cn);
           cmd.CommandType = CommandType.StoredProcedure;
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           da.Fill(dt);
           cn.Close();
           return dt;
       }

       public static bool ThucHienChuyenTien(int maGD,int trangthai,int type)
       {
           SqlConnection cn = DataProvider.Connect();
           SqlCommand cmd = new SqlCommand("Proc_GDNgoaiNH_Update", cn);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add(new SqlParameter("@maGD", maGD));
           cmd.Parameters.Add(new SqlParameter("@trangthai", trangthai));
           cmd.Parameters.Add(new SqlParameter("@type", type));
           cmd.ExecuteNonQuery();
           cn.Close();
           return true;
       }
    }
}
